package com.zwy.rxjava;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 使用Operators map 和 flatMap
 */
public class RxJavaDemo3 extends Activity implements OnClickListener {
	
	private TextView tvs;
	private TextView tve;
	private TextView tvc;
	
	private Button btn1;
	private Button btn2;
	private Button btn3;

	private List<MessageTask> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_rxjava2);

		LogUtils.allowD = true;
		LogUtils.allowE = true;
		LogUtils.customTagPrefix = "itchen";


		tvs = (TextView) findViewById(R.id.tvs_rx2);
		tve = (TextView) findViewById(R.id.tve_rx2);
		tvc = (TextView) findViewById(R.id.tvc_rx2);
		
		btn1 = (Button) findViewById(R.id.btn1_rx2);
		btn2 = (Button) findViewById(R.id.btn2_rx2);
		btn3 = (Button) findViewById(R.id.btn3_rx2);
		
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		
		Log.i("zwy", "1t:" + Thread.currentThread().getId());
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1_rx2:
			test1();
			break;
		case R.id.btn2_rx2:
			test2();
			break;
		case R.id.btn3_rx2:
			break;
		default:
			break;
		}
	}

	private void test1() {
		/**
		 * Func1(回调的参数，回调返回的参数)，即： public 回调返回的参数 call(回调的参数)
		 * 可以嵌套多个map进行操作
		 */
		Observable.just(new MessageEvent("Hello"))//
				.map(new Func1<MessageEvent, String>() {///
			@Override//
			public String call(MessageEvent arg0) {
				Log.i("zwy", "2t:" + Thread.currentThread().getId());//
				arg0.setMsg(arg0.getMsg() + " world");
				return arg0.getMsg();
			}
		}).map(new Func1<String, MessageEvent>() {
			@Override
			public MessageEvent call(String arg0) {
				Log.i("zwy", "3t:" + Thread.currentThread().getId());
				MessageEvent msg = new MessageEvent(arg0 + "!Welcome BeiJing");
				return msg;
			}

		}).subscribe(new Action1<MessageEvent>() {
			@Override
			public void call(MessageEvent arg0) {
				Log.i("zwy", "4t:" + Thread.currentThread().getId());
				tvs.setText(arg0.getMsg());
			}
		});
	}


	/*Observable.just(1, 2, 3, 4)
			.subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
			.observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
			.subscribe(new Action1<Integer>() {
		@Override
		public void call(Integer number) {
			Log.d(tag, "number:" + number);
		}
	});*/


	private void test2() {
		list = new ArrayList<MessageTask>();
		list.add(new MessageTask(0, "内容0"));
		list.add(new MessageTask(1, "内容1"));
		list.add(new MessageTask(2, "内容2"));
		list.add(new MessageTask(3, "内容3"));
		list.add(new MessageTask(4, "内容4"));

		//cast操作符就是将不同数据类型转换成指定类型.

		Observable.just(list).flatMap(new Func1<List<MessageTask>, Observable<?>>() {
			@Override
			public Observable<?> call(List<MessageTask> arg0) {
				return Observable.from(arg0);
			}
		})//
				.cast(MessageTask.class)//
				.map(new Func1<MessageTask, String>() {//
			@Override
			public String call(MessageTask s) {
				// 从集合中逐一取出操作
				String str = "";
				switch (s.getTag()) {
				case 0:
					str += s.getMsg() + "处理内容0";
					break;
				case 1:
					str += s.getMsg() + "处理内容1";
					break;
				case 2:
					str += s.getMsg() + "处理内容2";
					break;
				case 3:
					str += s.getMsg() + "处理内容3";
					break;
				case 4:
					str += s.getMsg() + "处理内容4";
					break;
				default:
					break;
				}
				return str;
			}
		}).subscribeOn(Schedulers.newThread())//指定被观察者发生在哪个线程
		.observeOn(AndroidSchedulers.mainThread())//指定观察者在哪个线程。
		.subscribe(new Action1<String>() {//
			@Override
			public void call(String s) {
				LogUtils.d("itchen--");
			}
		});

	}
}
